//
//  ShopGoodsViewController.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/23.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "ShopGoodsViewController.h"
#import "GoodsCenterHeaderView.h"
#import "GoodsTableViewHeaderView.h"
#import "GoodsTableViewCell.h"
#import "AddGoodsViewController.h"
#import "GoodsModel.h"
#import "MoneyViewController.h"
#import "MoneyDetailViewController.h"

@interface ShopGoodsViewController ()<UITableViewDelegate,UITableViewDataSource,GoodsTableViewHeaderViewDelegate,GoodsTableViewCellDelegate,GoodsCenterHeaderViewDelegate>

@property (nonatomic, weak)UIView * navView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UIButton * addButton;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <GoodsModel *> *goodsArray;
@property (nonatomic, copy)NSString * type;
@property (nonatomic, assign)int count;

@end

@implementation ShopGoodsViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadDataWithMerchants_goods_status:@"0,1,2,3,4"];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)setGoodsArray:(NSArray<GoodsModel *> *)goodsArray{
    
    _goodsArray = goodsArray;
    
    [self.tableView reloadData];
}


- (void)loadDataWithMerchants_goods_status:(NSString *)merchants_goods_status{
    
    NSString * shop_id = DEF_GET_OBJECT(@"shopid");
    
    [[ZRNetworkTool sharedTool] getGoodsListResultWithShopid:shop_id WithMerchants_goods_status:merchants_goods_status WithIs_merchants_get:@"1" WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.goodsArray = [NSArray yy_modelArrayWithClass:[GoodsModel class] json:responseData[@"data"]];
            
            if (self.count == 1) {
                
                NSArray* testResult = [NSArray bg_arrayWithName:@"shopGoods"];
                if (testResult.count > 0) {
                    
                    [NSArray bg_clearArrayWithName:@"shopGoods"];
                    [self.goodsArray bg_saveArrayWithName:@"shopGoods"];
                }else{
                    
                    [self.goodsArray bg_saveArrayWithName:@"shopGoods"];
                }
            }
            
            self.count = self.count + 1;
        }
      
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ZRWhiteColor;
    self.type = @"all";
    self.count = 1;
    self.goodsArray = [NSArray bg_arrayWithName:@"shopGoods"];
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    UIView * navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kStatusBarHeight + kNavHeight)];
    
    navView.backgroundColor = ColorWithRGB(0xFFE055);
    navView.userInteractionEnabled = YES;
    
    self.navView = navView;
    
    [self.view addSubview:self.navView];

    
    UIButton * addButton = [UIButton new];
    
    [addButton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(clickAddButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.addButton = addButton;
    
    [self.navView addSubview:self.addButton];
    
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = ZRBlackColor;
    titleLabel.font = [UIFont boldSystemFontOfSize:16 * SCREENH6];
    titleLabel.text = @"商品中心";
    
    self.titleLabel = titleLabel;
    
    [self.navView addSubview:self.titleLabel];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight + kNavHeight, kScreenW, kScreenH - kTabBarHeight - kStatusBarHeight - kNavHeight) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF8F9FA);
    tableView.rowHeight = 135 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    GoodsCenterHeaderView * headerView =  [[GoodsCenterHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 100 * SCREENH6)];
    headerView.goodsCenterHeaderViewDelegate = self;
    // 设置header
    tableView.tableHeaderView = headerView;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[GoodsTableViewCell class] forCellReuseIdentifier:@"GoodsTableViewCell"];
    [self.tableView registerClass:[GoodsTableViewHeaderView class] forHeaderFooterViewReuseIdentifier:@"GoodsTableViewHeaderView"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self updateData];
    }];
    
    [self.view addSubview:self.tableView];
}

- (void)viewDidLayoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(kStatusBarHeight + 10 * SCREENH6);
    }];
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.titleLabel);
        make.width.height.mas_equalTo(25 * SCREENH6);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.goodsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.goodsArray[indexPath.row];
    cell.goodsTableViewCellDelegate = self;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    GoodsTableViewHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"GoodsTableViewHeaderView"];
    
    header.goodsTableViewHeaderViewDelegate = self;
    
    return header;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
        ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = ColorWithRGB(0xF8F9FA);
    }
}

- (void)clickAddButton{
    
    HiddenBar;
    
    AddGoodsViewController * addVC = [AddGoodsViewController new];
    
    [self.navigationController pushViewController:addVC animated:YES];
    
    ShowBar;
}


- (void)clickItemButton:(NSString *)type{
    
    self.type = type;
    if ([type isEqualToString:@"all"]) {
        
        [self loadDataWithMerchants_goods_status:@"0,1,2,3,4"];
    }else if ([type isEqualToString:@"news"]){
        
        [self loadDataWithMerchants_goods_status:@"2"];
    }
}


- (void)clickItemButton:(UIButton *)type WithCell:(GoodsTableViewCell *)cell{
    
    NSString * ID = cell.ID;
    
    if (type == cell.editButton) {
        
        if (type.selected) {

            [type setBackgroundColor:ZRBlackColor];
            type.selected = NO;
            HiddenBar;
            
            AddGoodsViewController * addVC = [AddGoodsViewController new];
            
            addVC.ID = ID;
            
            [self.navigationController pushViewController:addVC animated:YES];
            
            ShowBar;

        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"是否下架该产品？"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      [[ZRNetworkTool sharedTool] changeGoodsStatusResultWithGoods_id:ID WithMerchants_goods_status:@"3" WithBlock:^(BOOL success, id responseData) {
                                                                          
                                                                          if (success) {
                                                                              
                                                                              [type setBackgroundColor:[UIColor lightGrayColor]];
                                                                              type.selected = YES;
                                                                                                                                                        cell.updateButton.hidden = NO;
                                                                              
                                                                          }
                                                                      }];
                                                                  }];
            UIAlertAction* cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                                 handler:^(UIAlertAction * action) {
                                                                     
                                                                 }];
            
            [alert addAction:defaultAction];
            [alert addAction:cancleAction];
            [self presentViewController:alert animated:YES completion:nil];
        }

    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"是否上架该产品？"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                                  [[ZRNetworkTool sharedTool] changeGoodsStatusResultWithGoods_id:ID WithMerchants_goods_status:@"2" WithBlock:^(BOOL success, id responseData) {
//                                                                      NSLog(@"===%@",responseData);
                                                                      if (success) {
                                                                          
                                                                          type.hidden = YES;
                                                                          cell.editButton.backgroundColor = ZRBlackColor;
                                                                          cell.editButton.selected = NO;

                                                                          
                                                                      }
                                                                  }];
                                                              }];
        UIAlertAction* cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * action) {
                                                                 
                                                             }];
        
        [alert addAction:defaultAction];
        [alert addAction:cancleAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
//    if ([type isEqualToString:@"putDown"]) {
    
    
        
        
//    }else if ([type isEqualToString:@"update"]){
//
//        [[ZRNetworkTool sharedTool] changeGoodsStatusResultWithGoods_id:ID WithMerchants_goods_status:@"2" WithBlock:^(BOOL success, id responseData) {
//
//            if (success) {
//
//            }
//        }];
//    }else if ([type isEqualToString:@"edit"]){
//
//        HiddenBar;
//
//        AddGoodsViewController * addVC = [AddGoodsViewController new];
//
//        addVC.ID = ID;
//
//        [self.navigationController pushViewController:addVC animated:YES];
//
//        ShowBar;
//    }
}

- (void)changeGoodsState{
    
    
}

- (void)clickHeaderItemButton:(NSString *)type WithMoney:(NSString *)money{
    
    HiddenBar;
    if ([type isEqualToString:@"verify"]) {
        
        MoneyViewController * moneyVC = [MoneyViewController new];
        
        moneyVC.money = money;
        moneyVC.type = @"2";
        
        [self.navigationController pushViewController:moneyVC animated:YES];
        
    }else if ([type isEqualToString:@"detail"]){
        
        MoneyDetailViewController * detailVC = [MoneyDetailViewController new];
        
        detailVC.type = @"2";
        
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }
    
    ShowBar;
}

- (void)updateData{
    
    if ([self.type isEqualToString:@"all"]) {
        
        [self loadDataWithMerchants_goods_status:@"0,1,2,3,4"];
    }else if ([self.type isEqualToString:@"news"]){
        
        [self loadDataWithMerchants_goods_status:@"2"];
    }
    [self.tableView.mj_header endRefreshing];
}

@end
